package com.platform.sys.modular.syncdata.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.platform.core.exception.ServiceException;
import com.platform.core.factory.PageFactory;
import com.platform.core.pojo.page.PageResult;
import com.platform.core.util.PoiUtil;
import com.platform.sys.modular.syncdata.entity.SyncDataTarget;
import com.platform.sys.modular.syncdata.enums.SyncDataTargetExceptionEnum;
import com.platform.sys.modular.syncdata.mapper.SyncDataTargetMapper;
import com.platform.sys.modular.syncdata.param.SyncDataTargetParam;
import com.platform.sys.modular.syncdata.result.TargetResult;
import com.platform.sys.modular.syncdata.service.SyncDataTargetService;

import java.util.List;

/**
 * 同步目标设置service接口实现类
 *
 * @author gyp
 * @date 2022-12-05 11:00:08
 */
@Service
public class SyncDataTargetServiceImpl extends ServiceImpl<SyncDataTargetMapper, SyncDataTarget> implements SyncDataTargetService {

    @Override
    public PageResult<TargetResult> page(SyncDataTargetParam syncDataTargetParam) {
        QueryWrapper<TargetResult> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(syncDataTargetParam)) {
            //根据表名称进行查询
            if (ObjectUtil.isNotEmpty(syncDataTargetParam.getTableName())) {
                queryWrapper.like("t.table_name",syncDataTargetParam.getTableName());
            }
        }
        Page<TargetResult> page = this.baseMapper.page(PageFactory.defaultPage(), queryWrapper);
        return new PageResult<>(page);
    }

    @Override
    public List<SyncDataTarget> list(SyncDataTargetParam syncDataTargetParam) {
        return this.list();
    }

    @Override
    public void add(SyncDataTargetParam syncDataTargetParam) {
        SyncDataTarget syncDataTarget = new SyncDataTarget();
        BeanUtil.copyProperties(syncDataTargetParam, syncDataTarget);
        this.save(syncDataTarget);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SyncDataTargetParam syncDataTargetParam) {
        this.removeById(syncDataTargetParam.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SyncDataTargetParam syncDataTargetParam) {
        SyncDataTarget syncDataTarget = this.querySyncDataTarget(syncDataTargetParam);
        BeanUtil.copyProperties(syncDataTargetParam, syncDataTarget);
        this.updateById(syncDataTarget);
    }

    @Override
    public SyncDataTarget detail(SyncDataTargetParam syncDataTargetParam) {
        return this.querySyncDataTarget(syncDataTargetParam);
    }

    /**
     * 获取同步目标设置
     *
     * @author gyp
     * @date 2022-12-05 11:00:08
     */
    private SyncDataTarget querySyncDataTarget(SyncDataTargetParam syncDataTargetParam) {
        SyncDataTarget syncDataTarget = this.getById(syncDataTargetParam.getId());
        if (ObjectUtil.isNull(syncDataTarget)) {
            throw new ServiceException(SyncDataTargetExceptionEnum.NOT_EXIST);
        }
        return syncDataTarget;
    }

    @Override
    public void export(SyncDataTargetParam syncDataTargetParam) {
        List<SyncDataTarget> list = this.list(syncDataTargetParam);
        PoiUtil.exportExcelWithStream("SyncDataTarget.xls", SyncDataTarget.class, list);
    }

}
